<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.type"
          clearable
          placeholder="活动类型"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.status"
          clearable
          placeholder="是否上线"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <date-range-picker
          v-model="modelEndDate"
          start-placeholder="活动开始时间"
          end-placeholder=""
          range-separator=""
          :fix-date.sync="query.endDate"
          class="date-item"
        />
        <date-range-picker
          v-model="modelStartDate"
          start-placeholder="活动结束时间"
          end-placeholder=""
          range-separator=""
          :fix-date.sync="query.startDate"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px" inline>
          <el-col :span="12">
            <el-form-item label="中文活动名称" prop="nameCn">
              <el-input v-model="form.nameCn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文活动名称" prop="nameEn">
              <el-input v-model="form.nameEn" style="width: 220px;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker
                v-model="form.startDate"
                placeholder="选择日期"
                type="datetime"
                :default-time="form.startDate"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker
                v-model="form.endDate"
                placeholder="选择日期"
                type="datetime"
                :default-time="form.endDate"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动类型" prop="type">
              <el-select v-model="form.type" filterable placeholder="请选择" style="width: 220px">
                <el-option
                  v-for="item in dict.activeType"
                  :key="item.id"
                  :label="item.dictValue"
                  :value="item.dictKey"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否上线" prop="status">
              <el-radio v-for="item in dict.true_false" :key="item.id" v-model="form.status" :label="item.dictKey">
                {{ item.dictValue }}
              </el-radio>
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        row-key="id"
        stripe
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="nameCn" label="活动名称"      >
          <template slot-scope="scope">
            <div>{{ scope.row.nameCn }}</div>
            <div>{{ scope.row.nameEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="活动类型"      >
          <template slot-scope="scope">
            {{ dict.label.activeType[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="startDate" label="日期"      >
          <template slot-scope="scope">
            <div>开始时间:{{ scope.row.startDate }}</div>
            <div>结束时间:{{ scope.row.endDate }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="是否上线"      >
          <template slot-scope="scope">
            <mall-switch
              :row="scope.row"
              :dict-data="dict.label.true_false"
              :row-data="scope.row.status"
              row-filed="status"
              to-true-character="上线"
              to-false-character="取消上线"
              :title="scope.row.nameCn"
            />
          </template>
        </el-table-column>
        <el-table-column label="商品设置"      >
          <template slot-scope="scope">
            <el-button
              v-permission="['admin','promotionActive:goods']"
              type="info"
              plain
              size="mini"
              @click="setGoods(scope.row.id,scope.row.nameCn)"
            >设置商品</el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin','promotionActive:edit','promotionActive:del'])"
          label="操作"
          width="150px"

        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPromotionActive from '@/api/sms/promotionActive'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import mallSwitch from '@/components/mallSwitch'

const defaultForm = { id: null, nameCn: null, nameEn: null, type: null, endDate: null, startDate: null, status: true }
export default {
  name: 'PromotionActive',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker, mallSwitch },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['activeType', 'true_false'],
  cruds() {
    return CRUD({ title: '优惠活动', url: 'promotionActive/page', sort: 'id:desc', crudMethod: { ...crudPromotionActive }})
  },
  data() {
    return {
      modelEndDate: '',
      modelStartDate: '',
      permission: {
        add: ['admin', 'promotionActive:add'],
        edit: ['admin', 'promotionActive:edit'],
        del: ['admin', 'promotionActive:del']
      },
      rules: {
        nameCn: [
          { required: true, message: '中文活动名称不能为空', trigger: 'blur' }
        ],
        nameEn: [
          { required: true, message: '英文活动名称不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '活动类型不能为空', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '结束日期不能为空', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '开始日期不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '是否上线不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'type', display_name: '活动类型' },
        { key: 'status', display_name: '是否上线' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    setGoods(activeId, activeName) {
      this.$router.push({ path: 'promotionActiveProductRelation/' + activeId, query: { id: activeId, name: activeName }})
    }
  }
}
</script>

<style scoped>

</style>
